#include "carrier.h"

const volatile UINT32 Bank1_Reg04 = 0x21869ED9;
const volatile UINT8  g_RF_FCC_Payload[32] = 
{ 
    0xa6, 0xe4, 0xad, 0x3f, 0x64,    0x96, 0xfc, 0x9a, 0x99, 0x80,
    0xc6, 0x51, 0xa5, 0xfd, 0x16,    0x3a, 0xcb, 0x3c, 0x7d, 0xd0,
    0x6b, 0x6e, 0xc1, 0x6b, 0xea,    0xa0, 0x52, 0xbc, 0xbb, 0x81, 
    0xce, 0x93   
};

/********************************************************************************** 
Funcname:            void TestRFCarrierOuput( 
                   					unsigned char CH, 
                					unsigned char Power, 
                					unsigned char AirRate,
                                    unsigned char DataFlag )
 
Parameter:           CH       [IN]    	Send channel 
                     Power    [IN] 	    Send rate
                     AirRate  [IN]      AirRate
                     DataFlag [IN]      Data send


 
Description: 
                测试RF发射输出，按照指定的频道，功率，数据传输速率及数据方式，发送RF信号
**********************************************************************************/
/*
void TestRFCarrierOuput(                                                                                    
    unsigned char CH,                                                                                       
    unsigned char Power,                                                                                    
    unsigned char AirRate,                                                                                  
    unsigned char DataFlag )                                                                                
{                                                                                                           
    unsigned int temp = 30;                                                                                 
    CLR_CE();                                                                                               
    SPI_Write_Reg( W_REGISTER | RF_CH, CH ); //Set send channel;                                            
                                                                                                            
    if( AirRate )                                                                                           
    {                                                                                                       
        CH = 0x19;          //AirRate = 2Mbps                                                               
    }                                                                                                       
    else                                                                                                    
    {                                                                                                       
        CH = 0x11;          //AirRate = 1Mbps                                                               
    }                                                                                                       
                                                                                                            
    switch( Power )                                                                                         
    {                                                                                                       
    case 0:                                                                                                 
        CH |= 0x00;         //-10dBm                                                                        
        break;                                                                                              
    case 1:                                                                                                 
        CH |= 0x02;         //-5dBm                                                                         
        break;                                                                                              
    case 2:                                                                                                 
        CH |= 0x04;         //0dBm                                                                          
        break;                                                                                              
    case 3:                                                                                                 
        CH |= 0x06;         //5dBm                                                                          
        break;                                                                                              
    }                                                                                                       
                                                                                                            
    SPI_Write_Reg( ( W_REGISTER | RF_SETUP ), CH );         //Set communication channel                     
    SPI_Write_Reg( ( W_REGISTER | CONFIG ), 0x02 );         //Set RF for Power up mode                      
    RF_SetCarrierOut( TRUE );                               //Set single Carrier mode                       
                                                                                                            
    //add by shawn, FW5.15.1                                                                                
    if ( DataFlag )                                                                                         
    {                                                                                                       
        SPI_Write_Buff(W_TX_PAYLOAD , g_RF_FCC_Payload, sizeof(g_RF_FCC_Payload)); // Writes data to TX FIFO
    }                                                                                                       
                                                                                                            
    SET_CE();                                                                                               
}                                                                                                           
                                                                                                            
*/

/*******************************************************************************************
Function:           void SetRSSI( UINT8 en, UINT8 level ) 
Parameter: 
                    en    [IN]  0, 关闭RSSI， 1，开启RSSI 
                    level [IN]  0: -97 dBm,2 dB/step; 15: -67 dBm
Return: 
                    None
Description: 
                   Set carrier mode 
********************************************************************************************/
void SetRSSI(UINT8 en, UINT8 level)
{
    UINT8 WriteArr[4];
    WriteArr[0] = (level << 2);
    if( en )
    {
        WriteArr[1] = 0x02;
    }
    else
    {
        WriteArr[1] = 0x06;
    }
    WriteArr[2] = 0x7f;
    WriteArr[3] = 0xa6;

    SwitchCFG(1);                                       //Switch to Bank1
    SPI_Write_Buf((WRITE_REG|0x05), &(WriteArr[0]), 4);
    SwitchCFG(0);                                       //Switch to Bank0
}



/**********************************************************************************
Function:               UINT8 RF_CheckChnFree( UINT8 chn ) 
Parameter: 
                        chn  -- Freq channel for check 
Return: 
                        TRUE  channel is busy 
                        FALSE channel is free
Describe: 
                        Check channel: chn is busy or free
**********************************************************************************/
unsigned int RF_CheckChnFree( UINT8 chn )
{
    UINT8 IsBusy;

    SetRSSI(TRUE, 8);   //Enable carrier detect

    //Set the detect channel
    RF_SET_CHN(chn);
    //Clear CD flag
    RF_GET_CD();

    IsBusy = FALSE;

    //Delay 100ms
    DelayMs( 100 );

    //Read the CD flag
    IsBusy = RF_GET_CD();

    //Diable the carrier detect
    SetRSSI(FALSE, 8); 

    return IsBusy;
}

/**************************************************************************************************
Function:				void CarrierDetect( UINT8 CH )
Parameter:
						CH	[IN]	measure frequency channel
Return:
						None
Description:

**************************************************************************************************/
void CarrierDetect( UINT8 CH )
{
    UINT8 temp = 0;
    temp = RF_CheckChnFree( CH );
    if( temp )
    {
        GLED_OFF();				//Light yellow LED,express busy status;
		YLED_ON();
    }
    else
    {
        YLED_OFF();				//Light green LED,express free status;
		GLED_ON();
	}
}
